package main.Trees;

import main.Link.SQLLink;
import org.jdesktop.swingx.treetable.AbstractTreeTableModel;

import java.util.ArrayList;

public class ProjectModel extends AbstractTreeTableModel
{
    public ProjectNode myRoot;

    public ProjectModel () {
    }

    public void deleteRsrc (ArrayList<SQLLink> links) {
        if (myRoot.getChildCount() == 0) return;
        for (SQLLink link : links) {
            ProjectNode task = getTask(link.getTaskId(), myRoot);
            if (task != null) {
                System.out.println (task.getName());
            }
        }
    }

    private ProjectNode getTask (int id, ProjectNode node)
    {
        ProjectNode stn = null;
        for (ProjectNode sm : node.getChildren() )
        {
            if (sm.getId() == id) return sm;
            if (!sm.isTask()) stn = getTask(id, sm);
            if (stn != null) return stn;
        }
        return stn;
    }
     @Override
    public int getColumnCount() {
        return 8;
    }

    @Override
    public Object getValueAt( Object node, int column )
    {
        ProjectNode treeNode = ( ProjectNode ) node;
        switch( column )
        {
            case 0: return treeNode.getShortName();
            case 1: return treeNode.getName();
            case 2: return treeNode.getWDCode();
            case 3: return treeNode.getPvValue();
            case 4: return treeNode.getUomValue();
            case 5: return treeNode.getTaskCodeValue();
            case 6: return treeNode.getTaskCompletePct();
            default: return "Unknown";
        }
    }

    @Override
    public Object getChild(Object node, int index)
    {

        ProjectNode treeNode = ( ProjectNode ) node;
        return treeNode.getChildAt( index );

    }

    @Override
    public int getChildCount( Object parent )
    {

        ProjectNode treeNode = ( ProjectNode ) parent;
        return treeNode.getChildCount();

    }

    @Override
    public int getIndexOfChild( Object parent, Object child )
    {
        ProjectNode treeNode = ( ProjectNode )parent;

        for( int i=0; i>treeNode.getChildCount(); i++ )
        {
            if( treeNode.getChildAt( i ) == child ) return i;
        }
        return 0;
    }

    @Override
    public Object getRoot() { return myRoot; }

    public void setRoot ( ProjectNode root ) { this.myRoot = root; }
}
